package arithmetic.LeetCode;

import groovy.json.JsonOutput;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

/**
 * @author jiangfeng 2019/7/26 15:49
 */
public class MergeSortedArray {

    public static void main(String[] args) {
        int[] nums1 = {1, 2, 3, 0, 0, 0}, nums2 = {2, 5, 6};
        int m = 3, n = 3;
        // merge(nums1,m,nums2,n);
        System.out.println(JsonOutput.toJson(nums1));

//-------------------------------------------------------------
        int[] nums11 = {1}, nums21 = {};
        int m1 = 1, n1 = 0;
        merge(nums11, m1, nums21, n1);
        System.out.println(JsonOutput.toJson(nums11));

        System.out.println(calcLastWordSize("123 fasdf  "));
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int newIndex = m + n - 1, i = m - 1, j = n - 1;
        while (newIndex > -1) {
            if (i == -1) {
                while (j > -1) {
                    nums1[newIndex--] = nums2[j--];
                }
                return;

            }
            if (j == -1) {
                while (i > -1) {
                    nums1[newIndex--] = nums1[i--];
                }
                return;
            }
            nums1[newIndex--] = nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
        }
    }

    public static int calcLastWordSize(String s) {
        if (s == null || s.length() < 1) {
            return 0;
        }
        String[] result = s.split(" +");
        return result[result.length - 1].length();
    }



    public static int maxProfit(int[] prices) {
        int n = prices.length;
        int dp0 = 0, dp1 = Integer.MIN_VALUE;
        int buy = 0, sell = 0, tempChange = 0;
        for (int i = 0; i < n; i++) {
            if (dp0 < dp1 + prices[i]) {
                dp0 = dp1 + prices[i];
                sell = i;//记录卖的那天
                buy = tempChange;//离卖的那天最近的买的一天
            }
            if (dp1 < -prices[i]) {
                dp1 = -prices[i];
                tempChange = i;
            }
        }
        System.out.println(buy);
        System.out.println(sell);
        return dp0;
    }

}
